package com.imagecollector.controller.common;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.imagecollector.domain.entity.Picture;
import com.imagecollector.domain.dto.PictureDTO;
import org.springframework.beans.BeanUtils;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.util.List;


public class PictureDTOAndPicture {

    private static final ObjectMapper mapper = new ObjectMapper();
    public static Picture PictureVoToPicture(PictureDTO pictureDTO) throws JsonProcessingException {
        Picture picture = new Picture();
        BeanUtils.copyProperties(pictureDTO,picture);
        List<String> urls = pictureDTO.getUrl();
        String jsonUrl = mapper.writeValueAsString(urls);
        picture.setUrl(jsonUrl);
        return picture;
    }

    public static String urlToString(List<String> urls) throws JsonProcessingException {
        return mapper.writeValueAsString(urls);
    }
    public static PictureDTO PictureToPictureVo(Picture picture) throws JsonProcessingException {
        PictureDTO pictureDTO = new PictureDTO();
        BeanUtils.copyProperties(picture, pictureDTO);
        String jsonUrl = picture.getUrl();
        List<String> urls = mapper.readValue(jsonUrl, new TypeReference<List<String>>(){});
        pictureDTO.setUrl(urls);
        return pictureDTO;
    }
}
